/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MidiGui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import midi.MidiPlayer;
import music.Chords.Chord;
import music.types.BaseNotes;
import music.types.Interval;
import music.types.MidiNote;
import music.types.NoteTypes;
/**
 *
 * @author TJ
 */
public class ChordConstructor extends JPanel {
    private JCheckBox[] intervals;
    private JComboBox root;
    private JSpinner octave;
    private Chord c;                                    
    ChordConstructor() {
        init();
    }
    public static void main(String[] args) {
        createFrame();
    }
    public static void createFrame() {
        JFrame j=new JFrame();
        //j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setSize(500,500);
        j.add(new ChordConstructor());
        j.setVisible(true);
    }
    private void init(){
        c=new Chord(NoteTypes.QUARTER);
        this.setLayout(new GridBagLayout());
        intervals=new JCheckBox[Interval.values().length];
        root=new JComboBox();
        for(BaseNotes n: BaseNotes.values()){
            root.addItem(n);
        }
        root.addActionListener(new IndexChange());
        add(new JLabel("Chord root:"),0,0,1,1);
        add(root,1,0,1,1);
       // c.setRoot((MidiNote)((BaseNotes)root.getSelectedItem()).getNote());
        int i=1,x=0;
        for(Interval s : Interval.values()) {
            intervals[i]=new JCheckBox();
            intervals[i].setText(s.getDiatonicName());
            intervals[i].addActionListener(new CheckListen());
//            intervals[i].setActionCommand(""+s.getInterval());
            add(intervals[i],x,i,1,1);
            i++;
            if(i>Interval.values().length/2+1){i=1;x+=1;}
        }
       
    }
    private void add(JComponent c2,int x, int y, int gw,int gh) {
        GridBagConstraints c=new GridBagConstraints();
        c.gridx=x;
        c.gridy=y;
        c.gridwidth=gw;
        c.gridheight=gh;
        this.add(c2,c);
    }
    public void playChord() {try{
            MidiPlayer.getMidiPlayer().playNote(c, 2000);
            }catch(Exception e2){JOptionPane.showMessageDialog(this, e2.toString());
            }
    }
        
    class IndexChange implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            
            //c.setRoot((MidiNote)((BaseNotes)((JComboBox)e.getSource()).getSelectedItem()).getNote());
            playChord();
        }
        
    }
    class CheckListen implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            JCheckBox c2=((JCheckBox)e.getSource());
            if(c2.isSelected()){
                //c.addInterval(Integer.parseInt(e.getActionCommand()));
            } else {
               // c.removeInterval(Integer.parseInt(e.getActionCommand()));
            }
            playChord();
            
        }
    }
}
